<?php
/**
 * Introduction: 发货地址逻辑
 * @author: 杨陈鹏
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Common\Logic;

use Common\Model;

class SendAddressLogic
{

    public static $model;

    public function __construct()
    {

        self::$model = new Model\SendAddressModel();

    }
    /**
     * Introduction: 获取数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function getData($where,$fields = '*')
    {


        $data = self::$model
                        ->where($where)
                        ->field($fields)
                        ->select();

        return ['code' => 200, 'data' => $data];


    }
    /**
     * Introduction: 获取一条数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function getOneData($id)
    {


        $data = self::$model
                        ->where(['id' => $id])
                        ->find();

        return ['code' => 200, 'data' => $data];


    }
    /**
     * Introduction: 插入数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function create($data)
    {

        if(!$data['name'])
            return ['code' => 300, 'msg' => '发货人不能为空!'];

        if(!$data['mobile'])
            return ['code' => 300, 'msg' => '联系电话不能为空!'];

        if(!$data['address'])
            return ['code' => 300, 'msg' => '发货地址不能为空!'];

            $data['is_default'] = $data['is_default'] ? $data['is_default'] : 2 ;

        if ($data['is_default'] == 1)
            self::$model->where(['is_default' => 1])->save(['is_default' => 2]);

        if(self::$model->add($data))
            return ['code' => 200, 'msg' => '添加成功'];

        return ['code' => 300, 'msg' => '添加失败!'];


    }

    /**
     * Introduction: 修改数据
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    static public function update($where,$data)
    {

        if ( !$where )
            return ['code' => 300, 'msg' => '缺少条件信息'];

        if ($data['is_default'] == 1)
            self::$model->where(['is_default' => 1])->save(['is_default' => 2]);


        $result = self::$model
                        ->where($where)
                        ->save($data);

        if($result !== false)
            return ['code' => 200, 'msg' => '更新成功'];

        return ['code' => 300, 'msg' => '更新失败'];

    }


}